/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * Client-side update subscriber.
 */
@SuppressWarnings("serial")
public class UpdateSubscriber extends UnicastRemoteObject implements IRmiUpdateSubscriber {
	
	
	/**
	 * Saves the index of the player whose turn is next.
	 */
	private int savedNextPlayerIndex;
	
	
	/**
	 * Saves the index of the player whose turn was last.
	 */
	private int savedLastPlayerIndex;
	
	
	/**
	 * Saves the index of the action which was executed in the last turn.
	 */
	private int savedLastPlayerActionLocation;
	
	
	/**
	 * Saves additional details of the action which was executed in the last turn.
	 */
	private int savedLastPlayerActionDetail;

	
	public UpdateSubscriber() throws RemoteException {
		super();
	}

	
	/**
	 * Server reports change in player list.
	 */
	@Override
	public void updatePlayerList() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				Connection.updatePlayerList();
				GUI.getGameSetupScreen().updatePlayerList();
			}
		});
	}


	/**
	 * Server reports the game has started.
	 */
	@Override
	public void startGame() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getGameSetupScreen().gameStarting();
			}
		});
	}


	/**
	 * Server reports a new season has begun.
	 */
	@Override
	public void newSeason() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getPanel().newSeason();
			}
		});
	}

	
	/**
	 * Server requests the player to update his money balance, buying agents and inventory.
	 */
	@Override
	public void updateMoneyBuyingAgentsInventory() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getPanel().updateMoneyBuyingAgentsInventory();
				Scenery.updateTowers();
			}
		});
	}


	/**
	 * Server requests the client to update the state of the client-side bank.
	 */
	@Override
	public void updateBank() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getBank().updateBank();
			}
		});	
	}


	/**
	 * Server requests the client to update the state of the client-side construction authority.
	 */
	@Override
	public void updateConstructionAuthority() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getConstructionAuthority().updateConstructionAuthority();
			}
		});
	}


	/**
	 * Server requests the client to update the state of the client-side tower element market.
	 */
	@Override
	public void updateTowerElementMarket() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getTowerElementMarket().updateTowerElementMarket();
			}
		});
	}


	/**
	 * Server requests the client to update the state of the client-side house of spies.
	 */
	@Override
	public void updateHouseOfSpies() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getHouseOfSpies().updateHouseOfSpies();
			}
		});
	}


	/**
	 * Server requests the client to update the state of the client-side palace of the caliph.
	 */
	@Override
	public void updatePalaceOfTheCaliph() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getPalaceOfTheCaliph().updatePalaceOfTheCaliph();
			}
		});
	}


	/**
	 * Server informs the client of the last turn and the player whose turn it now is.
	 */
	@Override
	public void nextPlayerTurn(int nextPlayerIndex, int lastPlayerIndex, int lastPlayerActionLocation, int lastPlayerActionDetail) throws RemoteException {
		this.savedNextPlayerIndex          = nextPlayerIndex;
		this.savedLastPlayerIndex          = lastPlayerIndex;
		this.savedLastPlayerActionLocation = lastPlayerActionLocation;
		this.savedLastPlayerActionDetail   = lastPlayerActionDetail;
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.getPanel().nextPlayerTurn(savedNextPlayerIndex, savedLastPlayerIndex, savedLastPlayerActionLocation, savedLastPlayerActionDetail);
				GUI.updateActionLocationState();
			}
		});
	}


	/**
	 * Server informs the client of the end of the season.
	 */
	@Override
	public void endOfSeason() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				//GUI.endOfSeason();
			}
		});
	}


	/**
	 * Server informs the client of the end of the game.
	 */
	@Override
	public void endOfGame() throws RemoteException {
		Game.executeAtNextUpdate(new ICallback() {
			public void run() {
				GUI.endOfGame();
			}
		});
	}

}
